﻿using System.Collections.Generic;
using LinqToAmazonSL.AWS;

namespace LinqToAmazonSL
{
    public class AmazonBookQueryCriteria : AmazonQueryCriteria
    {
        public AmazonBookQueryCriteria()
        {
            this.SearchIndex = SearchIndex.Books;
            Parameters = new Dictionary<string, QueryValue>();
            //Initialization of the stack where we save the operations to perform to the query
            OperationsStack = new Stack<Operations>();
        }

        //Generation of search criteria to books
        public override ItemSearchRequest[] ToAmazonRequest()
        {
            ItemSearchRequest[] requests = new ItemSearchRequest[1];
           
                requests[0] = new ItemSearchRequest() { SearchIndex = "Books" };

                if (Parameters.ContainsKey("Title"))
                    requests[0].Title = Parameters["Title"].Value;
                if (Parameters.ContainsKey("Author"))
                    requests[0].Author = Parameters["Author"].Value;
                if (Parameters.ContainsKey("Publisher"))
                    requests[0].Publisher = Parameters["Publisher"].Value;
                if (Parameters.ContainsKey("Condition"))
                {
                    Condition cond = Condition.All;
                    switch (Parameters["Condition"].Value)
                    {
                        case "Used": cond = Condition.Used;
                            break;
                        case "New": cond = Condition.New;
                            break;
                        case "Collectible": cond = Condition.Collectible;
                            break;
                        case "Refurbished": cond = Condition.Refurbished;
                            break;
                        default: cond = Condition.All;
                            break;
                    }
                    requests[0].Condition = cond;
                }
                if (Parameters.ContainsKey("MaximumPrice"))
                    requests[0].MaximumPrice = Parameters["MaximumPrice"].Value;
                if (Parameters.ContainsKey("MinimumPrice"))
                    requests[0].MinimumPrice = Parameters["MinimumPrice"].Value;

                if (Parameters.ContainsKey("OrderByDescending"))
                    if (Parameters["OrderByDescending"].Value.Equals("Price"))
                        requests[0].Sort = "-price";
                    else
                        if (Parameters["OrderByDescending"].Value.Equals("Title"))
                            requests[0].Sort = "-titlerank";

                if (Parameters.ContainsKey("OrderBy"))
                    if (Parameters["OrderBy"].Value.Equals("Price"))
                        requests[0].Sort = "price";
                    else
                        if (Parameters["OrderBy"].Value.Equals("Title"))
                            requests[0].Sort = "titlerank";

                if (Parameters.ContainsKey("Keywords"))
                    requests[0].Keywords = Parameters["Keywords"].Value;
                
                requests[0].ResponseGroup = new string[] { "Medium" };
                requests[0].ItemPage = "1";
            
            return requests;
        }

        //Cleanup criterion search
        public override AmazonQueryCriteria ClearCriteria()
        {
            return new AmazonBookQueryCriteria();
        }
    }
}
